#ifndef FASTBOOTTHREAD_H
#define FASTBOOTTHREAD_H

#include <QThread>
#include <QStringList>
#include <QQueue>
#include <QMutex>

typedef struct usb_handle usb_handle;
typedef struct usb_ifc_info usb_ifc_info;
struct FastbootOperation{
    enum {
        WriteOperation = 0, WrapOperation,
        RebootOperation, RunCommandOperation
    };
    int opType;

    QString serialNumber;
    QString partName;
    QString filePath;
};
class FastbootThread : public QThread
{
    Q_OBJECT
    enum {
        Exit, Idle, Busy, Canceled, MaxStatus
    } status;
    usb_handle *usb;
    unsigned short vendorId;
    QByteArray serialNumber;

    QStringList devList;

    QMutex locker;
    QQueue<FastbootOperation> opList;

public:
    explicit FastbootThread(QObject *parent = 0);

    bool isBusy() const { return status != Idle; }

    void writeImage(const QString &serialNumber,
                    const QString &partName, const QString &filePath);
    void wrapSection(const QString &serialNumber, const QString &partName);
    void runCommand(const QString &serialNumber, const QString &cmd);
    void cancelAndClearOperation();
    void rebootDevice(const QString &serialNumber);

protected:
    void run();

signals:
    void deviceListChanged(const QStringList &devList);
    void operationFinished();
    void logMessage(const QString &msg);

public slots:
    void stop();

protected:
    static int match_fastboot(usb_ifc_info *info, void *arg);
    static int list_devices_callback(usb_ifc_info *info, void *arg);

    void clearOperation();
    void appendOperation(const FastbootOperation &opCmd);
    bool getOperation(FastbootOperation &opCmd);
};

#endif // FASTBOOTTHREAD_H
